import JumpNumber from "./JumpNumber";


const {ccclass, property} = cc._decorator;

@ccclass
export default class ExampleFloatNum extends cc.Component {


    jumpNumberSystem: JumpNumber = null;

    onLoad() {
        // cocos creator注册鼠标点击事件监听
        this.jumpNumberSystem = this.node.getComponent(JumpNumber);
        if (!this.jumpNumberSystem) {
            this.jumpNumberSystem = this.node.addComponent(JumpNumber);
        }
        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
    }

    onDestroy() {
        // 移除鼠标点击事件监听，避免内存泄漏等问题
        this.node.off(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
    }

    onMouseDown(event) {
        //console.log("点击了");
        if (this.jumpNumberSystem) {
            // 获取鼠标点击在世界坐标系下的位置
            let position: cc.Vec2 = event.getLocation();
            let randomDamage: number = this.getRandomDamage();
            this.jumpNumberSystem.createJumpNumber(position.subtract(cc.v2(cc.winSize.width/2,cc.winSize.height/2)),randomDamage);
        }
    }

    // 生成900 - 9000之间的随机伤害值的方法
    getRandomDamage(): number {
        return Math.floor(Math.random() * (90000 - 900 + 1) + 900);
    }
}
